<template>
  <div class="div_padding">
    <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
      <el-form-item label="角色名称：" prop="name">
        <el-input v-model="formInline.name" placeholder="角色名称"></el-input>
      </el-form-item>
      <el-form-item label="权限字符：" prop="key">
        <el-input v-model="formInline.key" placeholder="权限字符"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" v-permission="'Search'" type="primary" @click="filterData">查询</el-button>
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')">重置</el-button>
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit">添加角色</el-button>
        <Insert ref="insert" @reloadTable="reloadTable"></Insert>
        <Edit ref="edit" @reloadTable="reloadTable"></Edit>
      </el-form-item>
      <el-button style="float: right" icon="el-icon-refresh-right" circle @click="reloadTable"></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="mini"
      height="72vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%;overflow-x: hidden;"
      row-key="id"
      border>
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        label="角色名称"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="key"
        label="权限标识"
        align="center"
        width="150">
      </el-table-column>
      <el-table-column
        prop="admin"
        label="是否管理员"
        align="center"
        width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.admin == true" style="color:#67C23A;">是</span>
          <span v-if="scope.row.admin == false" style="color:#909399;">否</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        align="center"
        sortable
        width="100">
      </el-table-column>
      <el-table-column prop="status" width="150" align="center" label="角色状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status == true" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.status == false" style="color:#909399;">停用</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        align="center"
        sortable
        width="150">
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作" align="center" width="300" prop="parent" fixed="right">
        <template slot-scope="scope">
            <el-button type="primary" v-permission="'Update'" size="mini" @click="onEdit(scope.$index,scope.row)" icon="el-icon-edit" plain>修改</el-button>
            <el-button type="danger" v-show="scope.row.key != 'admin'" v-permission="'Delete'" size="mini" icon="el-icon-delete" plain @click="onDelete(scope.row)">删除</el-button>
            <el-button type="warning" v-show="scope.row.key != 'admin'" size="mini" icon="el-icon-delete" plain @click="onSetting(scope.row)">配置权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 5px;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="cur_page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
  </div>
</template>
<script>
  import { getRoleData,delRoleData } from '@/api/role'
  import { successMsg,errorMsg } from '@/utils/tools'
  import Insert from './insert'
  import Edit from './edit'
  export default {
    name:'Role',
    components:{
      Insert,
      Edit
    },
    data() {
      return {
        showId:false,
        loading:true,
        tableData: [],
        total:0,
        cur_page:1,
        pageSize:10,
        formInline: {
          name: '',
          key: ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getRoleData({page:this.cur_page,limit:this.pageSize}).then(res=>{
          this.tableData=res.data.data
          this.total = res.data.total
          this.loading=false
        })
      },
      onSubmit(){
        this.$refs.insert.dialogFormVisible = true
      },
      onEdit(index,row){
        this.$refs.edit.dialogFormVisible = true
        this.$refs.edit.formInline = Object.assign({}, row);//formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
      },
      onDelete(row){
        this.$confirm('此操作将真实删除该角色,谨慎操作!是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          delRoleData(row.id).then(res=>{
            if(res.code===2000) {
              successMsg(res.msg)
              this.cur_page=1
              this.reloadTable()
            }
          })
        }).catch(()=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      reloadTable(){
        this.loading=true
        this.fetchData()
      },
      filterData(){
        let params = {}
        if(this.formInline.name){
          params.name=this.formInline.name
        }
        if(this.formInline.key){
          params.key=this.formInline.key
        }
        params.page=this.cur_page
        params.limit=this.pageSize
        console.log(params)
        this.loading=true
        getRoleData(params).then(res=>{
          this.tableData=res.data.data
          this.total = res.data.total  
          this.loading=false
        })
      },
      resetForm(formName){
        this.$refs[formName].resetFields()
        this.reloadTable()
      },
      handleSizeChange(val) {
        this.pageSize=val
        this.loading=true
        this.filterData()
      },
      handleCurrentChange(val) {
        this.cur_page=val
        this.loading=true
        this.filterData()
      },
      onSetting(row){
        this.$router.push({name:'rolePermission',params:{'title':row.name,'name':row.name,'key':row.key,'id':row.id}})
      }
    },
  }
</script>
<style lang="scss" scoped>
  .div_padding {
    padding: 10px 10px
  }
</style>
